package com.itheima.pay.controller;


import com.alibaba.fastjson.JSON;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.common.models.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeQueryResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeRefundResponse;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("easy")
public class EasyPayController {

    @Autowired
    Config config;

    @GetMapping("pay/{code}")
    public AlipayTradePrecreateResponse pay(@PathVariable String code) {
        Factory.setOptions(config);

        try {
            // 2. 发起API调用（以创建当面付收款二维码为例） 2234567890
            AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace().preCreate(
                    "商品信息",
                    code,
                    "0.1"
            );

            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                return response;
            } else {
                System.err.println("调用失败，原因：" + response.toString());
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }

        return null;
    }


    @GetMapping("querypay/{code}")
    public AlipayTradeQueryResponse querypay(@PathVariable String code) {
        Factory.setOptions(config);

        try {
            AlipayTradeQueryResponse response = Factory.Payment.Common().query(code);

//            }

           return response;

        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }

    }


    @GetMapping("tuikuan/{code}")
    public AlipayTradeRefundResponse tuikuan(@PathVariable String code) {
        Factory.setOptions(config);

        try {
            AlipayTradeRefundResponse response = Factory.Payment.Common().refund(code, "0.1");

//            }

            return response;

        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }

    }


    @GetMapping("tuikuan12/{code}")
    public AlipayTradeFastpayRefundQueryResponse tuikuan12(@PathVariable String code) {
        Factory.setOptions(config);

        try {
            AlipayTradeFastpayRefundQueryResponse refund = Factory.Payment.Common().queryRefund(code, code);

//            }

            return refund;

        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }

    }



}
